﻿using CodeBuilder.DTO;
using CodeBuilder.Entity;
using CodeBuilder.Reader;
using CodeBuilder.Repository;
using CodeBuilder.Service;
using CodeBuilder.ViewModel;
using Microsoft.Extensions.Configuration;
using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeBuilder
{
    public class CodeBuilderBiz
    {
        public IList<string> GetTables(string dbaseName)
        {
            IList<string> tables = new List<string>();
            IConfiguration config = new ConfigurationBuilder()
                    .SetBasePath(Directory.GetCurrentDirectory())
                    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                    .Build();
            string connectionString = config["ConnectionStrings:DBConnetion"] ?? string.Empty;
            #region read table
            MyReader readTable = new MyReader(connectionString);
            DataTable schemaTable = readTable.ReadTable($"ELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{dbaseName}';");
            #endregion
            foreach (DataRow fieldItem in schemaTable.Rows)
            {
                tables.Add(fieldItem.ToString());
            }
            return tables;
        }
        public void Run(string inputs)
        {

            IConfiguration config = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .Build();

            string connectionString = config["ConnectionStrings:DBConnetion"] ?? string.Empty;
            string dtoPath = config["FilePaths:DtoPath"] ?? AppContext.BaseDirectory;
            string entityPath = config["FilePaths:EntityPath"] ?? AppContext.BaseDirectory;
            string repositoryPath = config["FilePaths:RepositoryPath"] ?? AppContext.BaseDirectory;
            string servicePath = config["FilePaths:ServicePath"] ?? AppContext.BaseDirectory;
            string viewModelPath = config["FilePaths:ViewModelPath"] ?? AppContext.BaseDirectory;

            if (string.IsNullOrWhiteSpace(inputs))
                return;
            var inputList = inputs.Split(',');
            foreach (var input in inputList)
            {
                var filePath = Path.Combine(AppContext.BaseDirectory, input);
                #region read table
                MyReader readTable = new MyReader(connectionString);
                DataTable schemaTable = readTable.ReadTable($"SHOW COLUMNS FROM {input};");
                #endregion
                #region read index
                MyReader readIndex = new MyReader(connectionString);
                DataTable indexTable = readIndex.ReadTable($"SHOW INDEX FROM {input};");
                #endregion

                CreateEntityBase createEntityBase = new CreateEntityBase(input, filePath, schemaTable, indexTable);
                createEntityBase.Run();

                CreateEntity createEntity = new CreateEntity(input, entityPath, schemaTable, indexTable);
                createEntity.Run();

                CreateIBaseRepository createIBaseRepository = new CreateIBaseRepository(input, filePath);
                createIBaseRepository.Run();

                CreateIRepository createIRepository = new CreateIRepository(input, repositoryPath, schemaTable, indexTable);
                createIRepository.Run();

                CreateRepository createRepository = new CreateRepository(input, repositoryPath, schemaTable, indexTable);
                createRepository.Run();

                CreateBaseRepository createBaseRepository = new CreateBaseRepository(input, filePath);
                createBaseRepository.Run();

                CreateDtoBase createDtoBase = new CreateDtoBase(input, filePath, schemaTable);
                createDtoBase.Run();

                CreateDto createDto = new CreateDto(input, dtoPath, schemaTable);
                createDto.Run();

                CreateIBaseService createIBaseService = new CreateIBaseService(input, filePath);
                createIBaseService.Run();

                CreateIService createIService = new CreateIService(input, servicePath, schemaTable, indexTable);
                createIService.Run();

                CreateBaseService createBaseService = new CreateBaseService(input, filePath);
                createBaseService.Run();

                CreateService createService = new CreateService(input, servicePath, schemaTable, indexTable);
                createService.Run();

                CreateBaseViewModel createBaseViewModel = new CreateBaseViewModel(input, filePath);
                createBaseViewModel.Run();

                CreateViewModel createViewModel = new CreateViewModel(input, viewModelPath);
                createViewModel.Run();
            }

        }
    }
}
